package com.itmk.web.sys_lost.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itmk.utils.ResultUtils;
import com.itmk.utils.ResultVo;
import com.itmk.web.sys_lost.entity.SysLost;
import com.itmk.web.sys_lost.entity.SysLostParm;
import com.itmk.web.sys_lost.service.SysLostService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author java实战基地
 * @since 2023-10-21
 */
@RestController
@RequestMapping("/api/sysLost")
public class SysLostController {
    @Autowired
    private SysLostService sysLostService;

    //认领
    @PostMapping("/claim")
    public ResultVo claim(@RequestBody SysLost sysLost){
        sysLost.setStatus("1");
        if(sysLostService.updateById(sysLost)){
            return ResultUtils.success("记录成功!");
        }
        return ResultUtils.error("记录失败!");
    }

    //新增
    @PostMapping
    public ResultVo add(@RequestBody SysLost sysLost){
        sysLost.setCteateTime(new Date());
        sysLost.setStatus("0");
        if(sysLostService.save(sysLost)){
            return ResultUtils.success("新增成功!");
        }
        return ResultUtils.error("新增失败!");
    }

    //编辑
    @PutMapping
    public ResultVo edit(@RequestBody SysLost sysLost){
        if(sysLostService.updateById(sysLost)){
            return ResultUtils.success("编辑成功!");
        }
        return ResultUtils.error("编辑失败!");
    }

    //删除
    @DeleteMapping("/{lostId}")
    public ResultVo delete(@PathVariable("lostId") Long lostId){
        if(sysLostService.removeById(lostId)){
            return ResultUtils.success("删除成功!");
        }
        return ResultUtils.error("删除失败!");
    }

    //列表
    @GetMapping("/list")
    public ResultVo list(SysLostParm parm){
        //构造分页对象
        IPage<SysLost> page = new Page<>(parm.getCurrentPage(),parm.getPageSize());
        //构造查询条件
        QueryWrapper<SysLost> query = new QueryWrapper<>();
        query.lambda().eq(StringUtils.isNotEmpty(parm.getTitle()),SysLost::getTitle,parm.getTitle())
                .eq(StringUtils.isNotEmpty(parm.getType()),SysLost::getType,parm.getType())
                .orderByDesc(SysLost::getCteateTime);
        IPage<SysLost> list = sysLostService.page(page, query);
        return ResultUtils.success("查询成功",list);
    }
}

